package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class Patrons extends BaseRepair {

    public Patrons(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(){
        List<Map<String, String>> userListMap = new ArrayList<>();
        List<Map<String, String>> patronsList = new ArrayList<>();
        userListMap = getVipUser("200000");
        patronsList = getPatrons(userListMap);
        DataMgr.addData(userListMap);
    }

    public void go1(){
        List<Map<String, String>> userListMap = new ArrayList<>();
        List<Map<String, String>> patronsList = new ArrayList<>();
        userListMap = getVipUser("200000");
        patronsList = getPatrons(userListMap);
        DataMgr.addData(patronsList);
    }

    private List<Map<String, String>> getPatrons(List<Map<String,String>> userListMap) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,patronsId,stageLv FROM t_u_patrons WHERE userId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            for(Map<String,String> userData : userListMap){
                int i=1;
                call.setString(i++,userData.get("userId"));
                rs = call.executeQuery();
                while (rs.next()) {
                    String userId = checkNullToEmpty(rs.getString("userId"));
                    String patronsId = checkNullToEmpty(rs.getString("patronsId"));
                    Map<String,String> temp = new ConcurrentHashMap<>();
                    temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                    temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                    temp.put("patronsId",patronsId);
                    temp.put("精进等级",checkNullToEmpty(rs.getString("stageLv")));
                    temp.put("门客服装技能等级",getPatronSkillInfo(userId,patronsId,2));
                    temp.put("门客技能等级",getPatronSkillInfo(userId,patronsId,1));
                    dataList.add(temp);
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private String getPatronSkillInfo(String userId,String patronsId,int type) {
        String res = "";
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,patronsId,`type`,skillId,skillLv FROM t_u_patrons_skill " +
                "WHERE userId = ? AND patronsId = ?";
        if(type == 1){//所有技能
            sql = "SELECT userId,patronsId,`type`,skillId,skillLv FROM t_u_patrons_skill " +
                    "WHERE userId = ? AND patronsId = ?";
        }else if(type == 2){//和资质丹有关的皮肤技能
            sql = "SELECT userId,patronsId,`type`,skillId,skillLv FROM t_u_patrons_skill " +
                    "WHERE userId = ? AND patronsId = ? AND `type` = 1 AND skillId in(10030,10031,10032,10033,10034,80000,80001,80002,80003,80004,80005,80006,80007,80008,80009,80010,80011,80012,80013,80015,80016,80017,80018,80019,80020,80021,80023,80024,80025,80026,80027,80028,80029,80030,80031,80032)";
        }
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,userId);
            call.setString(i++,patronsId);
            rs = call.executeQuery();
            while (rs.next()) {
                res = res + rs.getString("skillId") + "=" + rs.getString("skillLv") + ";";
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return res;
    }

    private List<Map<String, String>> getVipUser(String vipExp) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,vipExp FROM t_u_user WHERE `vipExp` >= ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,vipExp);
            rs = call.executeQuery();
            while (rs.next()) {
                String userId = checkNullToEmpty(rs.getString("userId"));
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("vipExp",checkNullToEmpty(rs.getString("vipExp")));
                temp.put("资质丹使用数量",useGoodItem(userId,"110005"));
                temp.put("资质丹剩余数量",remainderGoodItem(userId,"110005"));

                temp.put("门客帖使用数量",useGoodItem(userId,"110011"));
                temp.put("门客帖剩余数量",remainderGoodItem(userId,"110011"));

                temp.put("门客残帖使用数量",useGoodItem(userId,"110012"));
                temp.put("门客残帖剩余数量",remainderGoodItem(userId,"110012"));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private String useGoodItem(String userId,String goodId) {
        BigInteger count = BigInteger.ZERO;
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,goodsId,isGet,num,nowNum,way FROM t_l_goods WHERE isGet = 0 AND userId = ? AND goodsId = ?";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,userId);
            call.setString(i++,goodId);
            rs = call.executeQuery();
            while (rs.next()) {
                BigInteger num = new BigInteger(rs.getString("num"));
                count = count.add(num);
                /*Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                dataList.add(temp);*/
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return count.toString();
//        return dataList;
    }

    private String getGoodItem(String userId,String goodId) {
        BigInteger count = BigInteger.ZERO;
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,goodsId,isGet,num,nowNum,way FROM t_l_goods WHERE isGet = 1 AND userId = ? AND goodsId = ?";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,userId);
            call.setString(i++,goodId);
            rs = call.executeQuery();
            while (rs.next()) {
                /*Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("vipExp",checkNullToEmpty(rs.getString("vipExp")));
                dataList.add(temp);*/
                BigInteger num = new BigInteger(rs.getString("num"));
                count = count.add(num);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return count.toString();
//        return dataList;
    }


    private String remainderGoodItem(String userId,String goodId) {
        BigInteger count = BigInteger.ZERO;
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,goodsId,num FROM t_u_bag WHERE userId = ? AND goodsId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,userId);
            call.setString(i++,goodId);
            rs = call.executeQuery();
            while (rs.next()) {
                /*Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("vipExp",checkNullToEmpty(rs.getString("vipExp")));
                dataList.add(temp);*/
                BigInteger num = new BigInteger(rs.getString("num"));
                return num.toString();
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }

        return count.toString();
//        return dataList;
    }

    public void goo(){
        List<Map<String, String>> patronsList = new ArrayList<>();
        patronsList = getPatrons2();
        DataMgr.addData(patronsList);
    }

    private List<Map<String, String>> getPatrons2() {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,patronsId,`level`,ability FROM t_u_patrons";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                String userId = checkNullToEmpty(rs.getString("userId"));
                String patronsId = checkNullToEmpty(rs.getString("patronsId"));
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",userId);
                temp.put("patronsId",patronsId);
                temp.put("等级",checkNullToEmpty(rs.getString("level")));
                temp.put("赚速",checkNullToEmpty(rs.getString("ability")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }
}
